import {Component, OnInit, Input} from '@angular/core';
import {HttpService} from '../../service/http.service';
import 'rxjs/add/operator/toPromise';
import {Book} from '../../module/book';
import {ActivatedRoute, Router} from '@angular/router';

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.css'],
})
export class HomeComponent implements OnInit {
  @Input() bookKey: String;
  bookList: Book[];
  hotBookList: Book[];
  selItem: Number = 0;


  constructor(private router: Router,
              private httpService: HttpService) {
  };

  navmove(index) {
    this.selItem = index;
  }

  ngOnInit() {
    this.httpService.getLastBookList().then(r => {
      this.bookList = r;
      for (let i = 0; i < this.bookList.length; i++) {
        const array = this.bookList[i].keywords.split(';');
        this.bookList[i].keywordArray = [];
        for (let j = 0; j < array.length; j++) {
          if (array[j] !== '') {
            this.bookList[i].keywordArray.push(array[j]);
          }
        }
      }

    });
    this.httpService.getHotBookList().then(r => {
      this.hotBookList = r;
      for (let i = 0; i < this.hotBookList.length; i++) {
        const array = this.hotBookList[i].keywords.split(';');
        this.hotBookList[i].keywordArray = [];
        for (let j = 0; j < array.length; j++) {
          if (array[j] !== '') {
            this.hotBookList[i].keywordArray.push(array[j]);
          }
        }
      }
    });
  }

  search() {
    console.log(this.bookKey);
    this.bookKey = this.bookKey.trim();
    if (this.bookKey != null && this.bookKey.length !== 0) {
      this.router.navigate(['search', this.bookKey]);
    }
  }

}
